package com.hp.controller;


import com.hp.pojo.User;
import com.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /*
    用户上传头像 用户
     */
    @RequestMapping("/uploadUserPhoto")
    public Map<String,Object> uploadUserPhoto(@RequestParam("userId") int userId,
                                    @RequestParam("photo") MultipartFile photo) throws IOException {
        return userService.uploadUserPhoto(userId, photo);
    }

    /*
    用户手机号是否唯一
     */
    @RequestMapping("/checkPhone")
    public Map<String,Object> checkPhone(@RequestParam("userPhone") String userPhone) throws IOException {
        return userService.checkPhone(userPhone);
    }

    /*
    验证用户登录是否成功 用户
    返回用户的数据
     */
    @PostMapping("/userLogin")
    public Map<String,Object> userLogin(String userPhone, String userPassword, HttpSession session){
        return userService.userLogin(userPhone,userPassword,session);
    }

    /*
    注册or新增一条用户记录 用户
     */
    @PostMapping("/addUser")
    public Map<String,Object> addUser(String userNickname, String userPassword,
                                      String userPhone, String userName, int userSex,String userPhoto,
                                      int userType,String idNumber){
        User user = new User();
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setUserPhone(userPhone);
        user.setUserSex(userSex);
        user.setUserNickname(userNickname);
        user.setUserType(userType);
        user.setIdNumber(idNumber);

        System.out.println(userNickname+userPassword+userPhone+userName+userSex);
        return userService.addUser(user);
    }

    /*
    根据用户id删除一条用户记录 用户
     */
    @PostMapping("/deleteUserById")
    public HashMap<String, Object> deleteUserById(@RequestParam("userId") int userId){
        return userService.deleteUserById(userId);
    }

    /*
    更新一条用户记录 用户
    */
    @PostMapping("/updateUserById")
    public HashMap<String, Object> updateUserById(@RequestParam("user") User user){
        return userService.updateUserById(user);
    }

    /*
    只更新一条用户记录的昵称，姓名，性别，电话 用户
    */
    @PostMapping("/updateUserPartById")
    public HashMap<String, Object> updateUserPartById(int userId,String userName,String userNickname,String userPhone,int userSex){
        return userService.updateUserPartById(userId,userNickname,userName,userPhone,userSex);
    }
    /*
    通过用户id获取用户记录信息 用户
     */
    @PostMapping("/getUserById")
    public HashMap<String, Object> getUserById(@RequestParam("userId") int userId){
        return userService.getUserById(userId);
    }

    /*
    获取所有用户记录 管理
     */
    @PostMapping("/getAllUsers")
    public HashMap<String, Object> getAllUsers(){
        return userService.getAllUsers();
    }

    /*
    通过用户昵称获取用户列表信息 管理
     */
    @PostMapping("/getUsersByNickname")
    public HashMap<String, Object> getUsersByNickname(@RequestParam("userNickname") String userNickname){
        return userService.getUsersByNickname(userNickname);
    }

    /*
    通过用户电话查找用户列表信息 管理
     */
    @PostMapping("/getUsersByPhone")
    public HashMap<String, Object> getUsersByPhone(@RequestParam("userPhone") String userPhone){
        return userService.getUsersByPhone(userPhone);
    }

    @PostMapping("/AliveUserById")
    public Map<String,Object> AliveUserById(int userId){
        return userService.AliveUserById(userId);
    }

    @PostMapping("/getAgent")
    public Map<String,Object> getAgent(){
        return userService.getAgent();
    }
}
